/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.flexengine.cursor;

import com.flexengine.logger.Logger;
import com.flexengine.utils.ResourceManager;
import com.flexengine.view.GameWindow;
import com.flexengine.view.Texture;
import java.awt.*;
import javax.swing.ImageIcon;

/**
 * @author Luann R. Athayde
 * @version 1.0
 * @since 1.0
 */
public class CursorManager {
    
    private static volatile boolean    showBasicCursor;
    private static volatile boolean    showGameCursor;
    private static volatile Robot      robot;
    private static volatile boolean    exclusiveMouse;
    private static volatile GameCursor gameCursor;
    
    static {
        showBasicCursor = true;
        showGameCursor  = false;
        gameCursor      = new GameCursor();
        exclusiveMouse  = true;
        try {
            robot = new Robot();
        } catch(AWTException e) {
            Logger.logWarning("CursorManager - static(): "+e.getMessage());
        }
    }

    public static void hideBasicCursor()  {
        GameWindow.getInstance().setCursor(
                Toolkit.getDefaultToolkit().createCustomCursor(
                Toolkit.getDefaultToolkit().getImage(""),
                new Point(0,0), "hideCursor"));
        showBasicCursor = false;
    }
    public static void showBasicCursor() {
        GameWindow.getInstance().setCursor(Cursor.getDefaultCursor());
        showBasicCursor = true;
    }
    public static void setBasicCursor(String image, String name) {
        Image img = new ImageIcon(ResourceManager.DEFAULT_PATH+image).getImage();
        Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(img, getGameCursor().getLocation(), name);
        GameWindow.getInstance().setCursor(cursor);
    }

    public static void hideGameCursor() {
        showGameCursor = false;
    }
    public static void showGameCursor() {
        if( showBasicCursor ) {
            hideBasicCursor();
        }
        showGameCursor = true;
    }

    public static GameCursor getGameCursor()  {
        return gameCursor;
    }

    public static void setGameCursor(String name, Texture img, Point loc)  {
        GameCursor cursor = getGameCursor();
        cursor.setName(name);
        cursor.setTexture(img);
        cursor.setLocation(loc);
        mouseMove(loc.x, loc.y);
    }

    public static boolean isShowBasicCursor()    {
        return showBasicCursor;
    }
    public static boolean isShowGameCursor()    {
        return showGameCursor;
    }
    
    public static void keepOnCenter() {
        GameWindow.getInstance().centeredCursor();
    }

    public static Texture getDefaultTextureCursor() {
        return getGameCursor().getTexture();
    }

    public static void setDefaultTextureCursor(Texture defaultTextureCursor) {
        getGameCursor().setTexture(defaultTextureCursor);
    }

    public static boolean isExclusiveMouse() {
        return exclusiveMouse;
    }

    public static void setExclusiveMouse(boolean exclusiveMouse) {
        CursorManager.exclusiveMouse = exclusiveMouse;
        getGameCursor().setExclusiveMode(exclusiveMouse);
    }
    
    public static void mouseMove(int x, int y) {
        try {
            robot.mouseMove(x, y);
        } catch(Exception e) {
            Logger.logWarning("CursorManager - mouseMove(): "+e.getMessage());
        }
    }
    
}